//
//  WowMainSyncSwitchStrategy.m
//  PersonalOCTest
//
//  Created by zlf on 2025/8/20.
//

#import "WowMainSyncSwitchStrategy.h"

@implementation WowMainSyncSwitchStrategy

- (BOOL)canHandleTag:(WowNotificationSwitchTag)tag {
    return tag == WowNotificationSwitchTagMainSync ||
           tag == WowNotificationSwitchTagThrottle ||
           tag == WowNotificationSwitchTagMute;
}

- (void)handleSwitchWithContext:(WowSwitchContext *)context {
    UISwitch *sender = context.sender;
    WowNotifiSetToDeviceManager *manager = context.manager;
    
    // 1. 设备连接检查
//    if (![manager handleSwitchAction:sender]) {
//        return;
//    }
    
    // 2. 获取对应的key
    NSString *key = [self keyForTag:sender.tag];
    if (!key.length) return;
    
    // 3. 更新本地状态
    [self updateLocalStateWithContext:context key:key value:sender.isOn];
    
    // 4. 发送设备命令
//    [manager mainSetWith:sender.isOn ? @YES : @NO
//                     key:key
//                callback:^(NSDictionary *param) {
//        // 处理回调结果
//        [self handleCallbackResult:param context:context];
//    }];
    
    // 5. 刷新UI
    if (context.reloadHandler) {
        context.reloadHandler();
    }
    
    // 6. 特殊处理
    [self handleSpecialLogicWithContext:context];
}

- (NSString *)keyForTag:(WowNotificationSwitchTag)tag {
    switch (tag) {
        case WowNotificationSwitchTagMainSync: return @"syncEnable";
        case WowNotificationSwitchTagThrottle: return @"throttleEnable";
        case WowNotificationSwitchTagMute: return @"muteEnable";
        default: return @"";
    }
}

- (void)updateLocalStateWithContext:(WowSwitchContext *)context key:(NSString *)key value:(BOOL)value {
    WowAllAppSetModel *tableViewModel = context.tableViewModel;
    
    if ([key isEqualToString:@"syncEnable"]) {
//        tableViewModel.syncEnable = value;
    } else if ([key isEqualToString:@"throttleEnable"]) {
//        tableViewModel.throttleEnable = value;
    } else if ([key isEqualToString:@"muteEnable"]) {
//        tableViewModel.muteEnable = value;
    }
}

- (void)handleSpecialLogicWithContext:(WowSwitchContext *)context {
    UISwitch *sender = context.sender;
    
    // 主开关特殊处理
    if (sender.tag == WowNotificationSwitchTagMainSync && sender.isOn) {
        // 显示蓝牙设置
        if ([context.viewController respondsToSelector:@selector(showNotifiBluetoothSetAlert)]) {
            [context.viewController performSelector:@selector(showNotifiBluetoothSetAlert)];
        }
        
        // 同步手机设置
        if (context.infoModel) {
//            BOOL phoneSetValue = context.tableViewModel.syncEnable && context.infoModel.setting.syncEnabled;
//            [context.manager phoneSetSyncWith:phoneSetValue ? @YES : @NO key:@"enable"];
        }
    }
}

- (void)handleCallbackResult:(NSDictionary *)param context:(WowSwitchContext *)context {
    // 处理回调结果，如错误处理、状态同步等
    NSNumber *code = param[@"code"];
    if (code && code.integerValue != 1) {
        // 处理失败情况
        NSLog(@"Switch operation failed with code: %@", code);
    }
}

- (NSInteger)priority { return 1; }

- (NSString *)strategyDescription { return @"主同步开关策略"; }

@end
